package twoD.hofem;

/**
 * 
 * @author Team 2
 * 
 */

public class LegendreFB extends AbstractPolynomialBasisOnR {

	/**
	 * Constructs a Legendre polynomial basis of degree 2.
	 */
	public LegendreFB() {

		setP(2);
	}

	/**
	 * Constructs a Legendre polynomial basis of the specified degree.
	 * 
	 * @param p
	 *            polynomial degree
	 */
	public LegendreFB(int p) {
		setP(p);
	}

	public FunctionRToR[] createBasis(int p) {

		PolynomialRToR[] shapeFunctions = new PolynomialRToR[p + 1];

		if (p >= 0) {
			shapeFunctions[0] = new PolynomialRToR(-1, 1, 1);
		}
		if (p >= 1) {
			shapeFunctions[1] = new PolynomialRToR(-1, 1, 0, 1);
		}
		if (p >= 2) {
			for (int i = 2; i < p + 1; i++) {



				PolynomialRToR f1 = ((PolynomialRToR)shapeFunctions[1]).multiply(2 * i - 1); 
				PolynomialRToR f2 = ((PolynomialRToR)shapeFunctions[i - 1]).multiply(f1);
				PolynomialRToR f3 = ((PolynomialRToR)shapeFunctions[i - 2].multiply(1-i));
				PolynomialRToR f4 = ((PolynomialRToR)f2.add(f3));
				shapeFunctions[i] = ((PolynomialRToR)f4.multiply(1.0/i));
				
			}

		}
		return shapeFunctions;
	}

	public String getName() {
		return "Legendre Polynomials";
	}

	public Interval getDomain() {

		return new Interval(-1, 1);
	}

}
